﻿using Models;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class D_SysConfig
    {
        public List<Sys_config> listCconfig()
        {
            //单表分页
            //var list = Sugar_helper.Db.Queryable<configs>().ToPageList(pagenumber, pageSize, ref totalCount);

            var list = SqlSugarHelper.Db.Queryable<Sys_config>().ToList();
            return list;
        }

        public int addList(List<Sys_config> configs)
        {
            var res = SqlSugarHelper.Db.Insertable(configs).ExecuteCommand();
            return res;
        }
        public bool del(List<Sys_config> list)
        {
            var res = SqlSugarHelper.Db.Deleteable(list).ExecuteCommand();            
            return res == list.Count ? true : false;
        }

        public bool saveList(List<Sys_config> sys_Configs)
        {
            var count = SqlSugarHelper.Db.Storageable<Sys_config>(sys_Configs).ExecuteCommand();
            return count == sys_Configs.Count ? true : false;
        }


        public bool teacherTimeout()
        {
            // NOTE:这里改Queryable where
            var res = SqlSugarHelper.Db.Ado.SqlQuery<Sys_config>("SELECT * FROM Sys_config WHERE teacher_deadline < GETDATE()");
            return res.Count == 1 ? true : false;
        }

        public bool stuTimeout()
        {
            var res = SqlSugarHelper.Db.Ado.SqlQuery<Sys_config>($"SELECT * FROM Sys_config WHERE stu_deadline < GETDATE()");
            return res.Count == 1 ? true : false;
        }
        public bool stuTimeout(Student student)
        {
            /*删除and major_id={student.major_id}*/
            var res = SqlSugarHelper.Db.Ado.SqlQuery<Sys_config>($"SELECT * FROM [dbo].[4_1_Sys_config] WHERE stu_deadline < GETDATE()");
            return res.Count == 1 ? true : false;
        }
        public List<Sys_config> teacherTimeout(Teacher teacher)
        {
            var res = SqlSugarHelper.Db.Queryable<Sys_config>().Where(conf => SqlFunc.Subqueryable<Teacher>()
                                                            .Where(T => T.teacher_id == teacher.teacher_id).Any()).ToList();

            return res;
        }
    }
}
